
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="Python">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>cutout &#8212; deepaugment 0.2.0 documentation</title>
    <link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
   
  <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for cutout</h1><div class="highlight"><pre>
<span></span><span class="c1"># copy-pasted from https://github.com/tensorflow/models/blob/master/research/autoaugment/augmentation_transforms.py</span>


<span class="kn">import</span> <span class="nn">random</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>


<div class="viewcode-block" id="create_cutout_mask"><a class="viewcode-back" href="../lib.cutout.html#cutout.create_cutout_mask">[docs]</a><span class="k">def</span> <span class="nf">create_cutout_mask</span><span class="p">(</span><span class="n">img_height</span><span class="p">,</span> <span class="n">img_width</span><span class="p">,</span> <span class="n">num_channels</span><span class="p">,</span> <span class="n">size</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Creates a zero mask used for cutout of shape `img_height` x `img_width`.</span>
<span class="sd">  Args:</span>
<span class="sd">    img_height: Height of image cutout mask will be applied to.</span>
<span class="sd">    img_width: Width of image cutout mask will be applied to.</span>
<span class="sd">    num_channels: Number of channels in the image.</span>
<span class="sd">    size: Size of the zeros mask.</span>
<span class="sd">  Returns:</span>
<span class="sd">    A mask of shape `img_height` x `img_width` with all ones except for a</span>
<span class="sd">    square of zeros of shape `size` x `size`. This mask is meant to be</span>
<span class="sd">    elementwise multiplied with the original image. Additionally returns</span>
<span class="sd">    the `upper_coord` and `lower_coord` which specify where the cutout mask</span>
<span class="sd">    will be applied.</span>
<span class="sd">  &quot;&quot;&quot;</span>
    <span class="k">assert</span> <span class="n">img_height</span> <span class="o">==</span> <span class="n">img_width</span>

    <span class="c1"># Sample center where cutout mask will be applied</span>
    <span class="n">height_loc</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="n">img_height</span><span class="p">)</span>
    <span class="n">width_loc</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="n">img_width</span><span class="p">)</span>

    <span class="c1"># Determine upper right and lower left corners of patch</span>
    <span class="n">upper_coord</span> <span class="o">=</span> <span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">height_loc</span> <span class="o">-</span> <span class="n">size</span> <span class="o">//</span> <span class="mi">2</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">width_loc</span> <span class="o">-</span> <span class="n">size</span> <span class="o">//</span> <span class="mi">2</span><span class="p">))</span>
    <span class="n">lower_coord</span> <span class="o">=</span> <span class="p">(</span>
        <span class="nb">min</span><span class="p">(</span><span class="n">img_height</span><span class="p">,</span> <span class="n">height_loc</span> <span class="o">+</span> <span class="n">size</span> <span class="o">//</span> <span class="mi">2</span><span class="p">),</span>
        <span class="nb">min</span><span class="p">(</span><span class="n">img_width</span><span class="p">,</span> <span class="n">width_loc</span> <span class="o">+</span> <span class="n">size</span> <span class="o">//</span> <span class="mi">2</span><span class="p">),</span>
    <span class="p">)</span>
    <span class="n">mask_height</span> <span class="o">=</span> <span class="n">lower_coord</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">upper_coord</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">mask_width</span> <span class="o">=</span> <span class="n">lower_coord</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">upper_coord</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
    <span class="k">assert</span> <span class="n">mask_height</span> <span class="o">&gt;</span> <span class="mi">0</span>
    <span class="k">assert</span> <span class="n">mask_width</span> <span class="o">&gt;</span> <span class="mi">0</span>

    <span class="n">mask</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="n">img_height</span><span class="p">,</span> <span class="n">img_width</span><span class="p">,</span> <span class="n">num_channels</span><span class="p">))</span>
    <span class="n">zeros</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">mask_height</span><span class="p">,</span> <span class="n">mask_width</span><span class="p">,</span> <span class="n">num_channels</span><span class="p">))</span>
    <span class="n">mask</span><span class="p">[</span><span class="n">upper_coord</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="p">:</span> <span class="n">lower_coord</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">upper_coord</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="p">:</span> <span class="n">lower_coord</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">zeros</span>
    <span class="k">return</span> <span class="n">mask</span><span class="p">,</span> <span class="n">upper_coord</span><span class="p">,</span> <span class="n">lower_coord</span></div>


<div class="viewcode-block" id="cutout_numpy"><a class="viewcode-back" href="../lib.cutout.html#cutout.cutout_numpy">[docs]</a><span class="k">def</span> <span class="nf">cutout_numpy</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">16</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Apply cutout with mask of shape `size` x `size` to `img`.</span>
<span class="sd">  The cutout operation is from the paper https://arxiv.org/abs/1708.04552.</span>
<span class="sd">  This operation applies a `size`x`size` mask of zeros to a random location</span>
<span class="sd">  within `img`.</span>
<span class="sd">  Args:</span>
<span class="sd">    img: Numpy image that cutout will be applied to.</span>
<span class="sd">    size: Height/width of the cutout mask that will be</span>
<span class="sd">  Returns:</span>
<span class="sd">    A numpy tensor that is the result of applying the cutout mask to `img`.</span>
<span class="sd">  &quot;&quot;&quot;</span>
    <span class="n">img_height</span><span class="p">,</span> <span class="n">img_width</span><span class="p">,</span> <span class="n">num_channels</span> <span class="o">=</span> <span class="p">(</span><span class="n">img</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">img</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">img</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
    <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">img</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3</span>
    <span class="n">mask</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">create_cutout_mask</span><span class="p">(</span><span class="n">img_height</span><span class="p">,</span> <span class="n">img_width</span><span class="p">,</span> <span class="n">num_channels</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">img</span> <span class="o">*</span> <span class="n">mask</span></div>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../index.html">deepaugment</a></h1>








<h3>Navigation</h3>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../index.html">Documentation overview</a><ul>
  <li><a href="index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2019, Baris Ozmen.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.8.3</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.12</a>
      
    </div>

    

    
  </body>
</html>